using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace Cartoon
{
    public class Config
    {
        private Config()
        {
        }

        public void Load()
        {
            if (!File.Exists(FileName))
                return;

            try
            {
                XmlReader reader = XmlReader.Create(FileName);
                while (!reader.EOF)
                {
                    if (reader.NodeType != XmlNodeType.Element)
                    {
                        reader.Read();
                        continue;
                    }
                    string value;
                    switch (reader.Name)
                    {
                        case SaveDirectoryString:
                            m_SaveDirectory = reader.ReadString();
                            break;

                        case Favorite:
                            value = reader.ReadString();
                            m_FavoriteList.Add(value);
                            break;

                        case DownHistory:
                            value = reader.ReadString();
                            m_DownHistoryList.Add(value);
                            break;

                        case Finish:
                            value = reader.ReadString();
                            m_FinishList.Add(value);
                            break;

                        case UnFinish:
                            value = reader.ReadString();
                            m_UnFinishList.Add(value);
                            break;

                        default:
                            break;
                    }
                    reader.Read();
                }
            }
            catch
            {
                Global.OnMessage(System.Windows.Forms.ToolTipIcon.Error, Strings.ConfigFileError);
            }            
        }

        public void Save()
        {
            if (string.IsNullOrEmpty(m_SaveDirectory) && m_FavoriteList.Count == 0 && m_FinishList.Count == 0 && m_UnFinishList.Count == 0)
                return;

            XmlWriter writer = XmlWriter.Create(FileName);
            writer.WriteStartElement("Cartoon");
            writer.WriteAttributeString("url", "http://www.886s.com");

            foreach (string value in m_FavoriteList)
            {
                writer.WriteStartElement(Favorite);
                writer.WriteValue(value);
                writer.WriteEndElement();
            }

            foreach (string value in DownHistoryList)
            {
                writer.WriteStartElement(DownHistory);
                writer.WriteValue(value);
                writer.WriteEndElement();
            }

            foreach (string value in m_FinishList)
            {
                writer.WriteStartElement(Finish);
                writer.WriteValue(value);
                writer.WriteEndElement();
            }

            foreach (string value in m_UnFinishList)
            {
                writer.WriteStartElement(UnFinish);
                writer.WriteValue(value);
                writer.WriteEndElement();
            }  

            writer.WriteStartElement(SaveDirectoryString);
            writer.WriteValue(m_SaveDirectory);
            writer.WriteEndElement();

            writer.WriteEndElement();
            writer.Close();
        }

        public string SaveDirectory
        {
            get { return m_SaveDirectory; }
            set { m_SaveDirectory = value; }
        }

        public ICollection<string> FavoriteList
        {
            get { return m_FavoriteList; }
        }

        public ICollection<string> FinishList
        {
            get { return m_FinishList; }
        }

        public ICollection<string> UnFinishList
        {
            get { return m_UnFinishList; }
        }

        public ICollection<string> DownHistoryList
        {
            get { return m_DownHistoryList; }
        }

        List<string> m_FavoriteList = new List<string>();
        List<string> m_DownHistoryList = new List<string>();
        List<string> m_FinishList = new List<string>();
        List<string> m_UnFinishList = new List<string>();
        string m_SaveDirectory = string.Empty;
        public const string FileName = "Config.xml";
        const string Favorite = "Favorite";
        const string DownHistory = "DownHistory";
        const string Finish = "Finish";
        const string UnFinish = "UnFinish";
        const string SaveDirectoryString = "SaveDirectory";

        public static Config Instance
        {
            get
            {
                if (m_Config == null)
                    m_Config = new Config();
                return m_Config;
            }
        }

        static Config m_Config = null;
    }
}
